//
//  HHYCustomVC.m
//  大股票
//
//  Created by apple on 2019/8/14.
//  Copyright © 2019 apple. All rights reserved.
//

#import "HHYHangQingVC.h"
//V
#import "HHYTopTitleView.h"
#import "HHYCustomTopView.h"
//VC
#import "HHYStockListVC.h"


@interface HHYHangQingVC ()<UIScrollViewDelegate>
{
    NSInteger _selectedTopTitleViewType;//显示模式
}

@property (nonatomic, strong) HHYCustomTopView *topSelectedView;

@property (nonatomic, strong) HHYTopTitleView *topTitleView;

@property (nonatomic, strong) UIButton *statusBtn;
//股票列表数组
@property (nonatomic, strong) HHYStockListVC *marketVC;
@property (nonatomic, strong) HHYStockListVC *gradeVC;
//侧滑sv
@property (nonatomic, strong) UIScrollView *sideslipSV;

@end

@implementation HHYHangQingVC

- (instancetype)init {
    self = [super init];
    if (self) {
        _selectedTopTitleViewType = 0;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"行情";
    //子view
    [self setupSubViews];
    [self updateSubViewFrame];
    [self updateUI];
}

- (void)setupSubViews {
    [self.view addSubview:self.topSelectedView];
    [self.view addSubview:self.topTitleView];
    [self.view addSubview:self.sideslipSV];
    [self addChildVC];
    
    [self.marketVC.view addSubview:self.statusBtn];

    
    [self.view bringSubviewToFront:self.statusBtn];

}

- (void)updateUI {
    [self changeTopViewTitleArrWithIndex:0];
}

- (void)updateSubViewFrame {
    //总高度 （含 导航栏及tabbar）
    CGFloat totalHeight = hhy_kScreenHeight - hhy_kStatusBarAndNavigationBarHeight - hhy_kTabbarHeight;
    
    self.topSelectedView.xl_y = 0;
    self.topTitleView.xl_y = CGRectGetMaxY(self.topSelectedView.frame);
    
    
    CGFloat sideslipViewOriY = CGRectGetMaxY(self.topTitleView.frame);
    self.sideslipSV.frame = CGRectMake(0, sideslipViewOriY, hhy_kScreenWidth, totalHeight - sideslipViewOriY);
    self.sideslipSV.contentSize = CGSizeMake(hhy_kScreenWidth*2, 0);
    
    self.marketVC.view.frame = self.sideslipSV.bounds;
    self.gradeVC.view.frame = self.sideslipSV.bounds;
    
    self.gradeVC.view.xl_centerX = hhy_kScreenWidth*1.5f;


    self.statusBtn.frame = CGRectMake(self.marketVC.view.xl_width - 56 - 15, self.marketVC.view.xl_height - 56 - 15, 56, 56);

}

#pragma mark - 加入子vc
- (void)addChildVC {
    [self.sideslipSV addSubview:self.marketVC.view];
    [self addChildViewController:self.marketVC];
    
    
    [self.sideslipSV addSubview:self.gradeVC.view];
    [self addChildViewController:self.gradeVC];
}

#pragma mark - btnActions
- (void)btnActions:(UIButton *)btn {
    if (@available(iOS 11.0, *)) {
        //防止多次触发事件
        __block UIButton *blockBtn = btn;
        blockBtn.userInteractionEnabled = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            blockBtn.userInteractionEnabled = YES;
        });
    }
    
    if (btn == self.statusBtn) {
        NSInteger index = _selectedTopTitleViewType==0?1:0;
        [self changeTopViewTitleArrWithIndex:index];
        return;
    }
    
}

#pragma mark - UIScrollViewDelegate

#pragma mark - 切换toptitleView 标题
- (void)changeTopViewTitleArrWithIndex:(NSInteger)index {
    _selectedTopTitleViewType = index;
    switch (index) {
        case 0:
            {
            self.topTitleView.titleArr = @[@"交易产品", @"卖出价", @"买入价"];
            }
            break;
        case 1:
        {
            self.topTitleView.titleArr = @[@"交易产品", @"最新价", @"涨跌幅"];
        }
            break;
        default:
            break;
    }
    
    //对子vc显示模式进行重置
    
}

//选择顶部按钮
- (void)selectedTopViewWithIndex:(NSInteger)index {
    //滑动SV
    [self.sideslipSV setContentOffset:CGPointMake(hhy_kScreenWidth*index, 0) animated:YES];
    
    switch (index) {
        case 0:
        {
            [self changeTopViewTitleArrWithIndex:_selectedTopTitleViewType];
        }
            break;
        case 1:
        {
            self.topTitleView.titleArr = @[@"交易产品", @"成交价", @"成交额"];
        }
            break;
        default:
            break;
    }
}

#pragma mark - getter
- (HHYStockListVC *)marketVC {
    if (!_marketVC) {
        _marketVC = [[HHYStockListVC alloc] initWithType:HHYStockListVCTypeCustom shouldShowPlaceHodlerView:NO];
        _marketVC.displayType = HHYStockListTCellDisplayType1;
    }
    return _marketVC;
}

- (HHYStockListVC *)gradeVC {
    if (!_gradeVC) {
        _gradeVC = [[HHYStockListVC alloc] initWithType:HHYStockListVCTypeCustom shouldShowPlaceHodlerView:NO];
        _gradeVC.displayType = HHYStockListTCellDisplayType3;
    }
    return _gradeVC;
}

- (UIScrollView *)sideslipSV {
    if (!_sideslipSV) {
        _sideslipSV = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _sideslipSV.delegate = self;
        _sideslipSV.backgroundColor = UIColor.whiteColor;
        _sideslipSV.showsHorizontalScrollIndicator = NO;
        _sideslipSV.showsVerticalScrollIndicator = NO;
        _sideslipSV.scrollEnabled = NO;
    }
    return _sideslipSV;
}

- (HHYCustomTopView *)topSelectedView {
    if (!_topSelectedView) {
        _topSelectedView = [[HHYCustomTopView alloc] initWithFrame:CGRectMake(0, 0, hhy_kScreenWidth, 36)];
        __weak typeof(self)wSelf = self;
        _topSelectedView.handler = ^(NSInteger selectedIndex) {
            __strong typeof(self)sSelf = wSelf;
            [sSelf selectedTopViewWithIndex:selectedIndex];
        };
    }
    return _topSelectedView;
}

- (HHYTopTitleView *)topTitleView {
    if (!_topTitleView) {
        _topTitleView = [[HHYTopTitleView alloc] initWithFrame:CGRectMake(0, 0, hhy_kScreenWidth, 40)];
    }
    return _topTitleView;
}

- (UIButton *)statusBtn {
    if (!_statusBtn) {
        _statusBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _statusBtn.frame = CGRectMake(8, 23, 56, 56);
//        _statusBtn.tintColor = WHITE_COLOR;
        [_statusBtn setBackgroundImage:[[UIImage imageNamed:@"切换查看方式"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [_statusBtn addTarget:self action:@selector(btnActions:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _statusBtn;
}

@end
